@extends('admin::layouts.master')

@section('title', __('seller::shipping.title'))

@section('content')
@if ($errors->has('error'))
<x-seller-alert type="danger" msg="{{ $errors->first('error') }}" class="mt-4" />
@endif

@if (session()->has('success'))
<x-seller-alert type="success" msg="{{ session('success') }}" class="mt-4" />
@endif

<div class="card">
  <div class="card-body h-min-600">
    <div class="d-flex justify-content-between mb-4">
      <a href="{{ admin_route('shipping.create') }}" class="btn btn-primary">{{ __('common.add') }}</a>
    </div>
    <div class="table-push">
      @if (count($shipping))
      <table class="table">
        <thead>
          <tr>
            <th>ID</th>
            <th>{{ __('seller::shipping.text_region') }}</th>
            <th>{{ __('seller::shipping.text_type') }}</th>
            <th>{{ __('seller::shipping.text_method') }}</th>
            <th>{{ __('seller::shipping.text_priority') }}</th>
            <th>{{ __('common.status') }}</th>
            <th>{{ __('common.created_at') }}</th>
            <th>{{ __('common.updated_at') }}</th>
            <th class="text-end">{{ __('common.action') }}</th>
          </tr>
        </thead>
        <tbody>
          @foreach ($shipping as $item)
          <tr>
            <td>{{ $item['id'] }}</td>
            <td>
              @foreach ($regions as $region)
                @if ($region->id == $item->region_id)
                {{ $region->name }}
                @endif
              @endforeach
            </td>
            <td>{{ $item['type'] == 'by_flat' ? __('seller::shipping.by_flat') : __('seller::shipping.by_weight') }}</td>
            <td>{{ $item['method'] == 'range' ? __('seller::shipping.text_range') : __('seller::shipping.text_cumulative') }}</td>
            <td>{{ $item['priority'] }}</td>
            <td class="{{ $item['status'] ? 'text-success' : 'text-secondary' }}">
              {{ $item['status'] ? __('common.enable') : __('common.disable') }}
            </td>
            <td>{{ $item['created_at'] }}</td>
            <td>{{ $item['updated_at'] }}</td>
            <td class="text-end">
              <a href="{{ admin_route('shipping.edit', [$item['id']]) }}" class="btn btn-outline-secondary btn-sm">{{
                __('common.edit') }}</a>
              <button class="btn btn-outline-danger btn-sm delete-btn" type='button' data-id="{{ $item['id'] }}">{{
                __('common.delete') }}</button>
            </td>
          </tr>
          @endforeach
        </tbody>
      </table>
      @else
      <div>
        <x-seller-no-data />
      </div>
      @endif
    </div>
  </div>
</div>
@endsection

@push('footer')
<script>
  $('.delete-btn').click(function(event) {
    const id = $(this).data('id');
    const self = $(this);

    layer.confirm('{{ __('common.confirm_delete') }}', {
      title: "{{ __('common.text_hint') }}",
      btn: ['{{ __('common.cancel') }}', '{{ __('common.confirm') }}'],
      area: ['400px'],
      btn2: () => {
        $http.delete(`shipping/${id}`).then((res) => {
          layer.msg(res.message);
          window.location.reload();
        })
      }
    })
  });
</script>
@endpush
